package cn.nbt.busManage.dao.mapper;

import cn.nbt.busManage.model.entity.Point;
import cn.nbt.busManage.model.entity.Role;
import cn.nbt.busManage.model.entity.User;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Mapper
public interface UserMapper {

    @Select("select * from bus_user")
    List<User> findAll();

    @Select("select * from bus_user where bus_user.user_name like '%${query}%' or bus_user.id like '%${query}%'")
    List<User> findByQuery(String query);

    @Select("select * from bus_user where bus_user.id = #{id}")
    User findById(@Param("id") Integer id);

    @Select("select * from bus_user where bus_user.user_name = #{userName}")
    User findByUserName(String userName);


    /**
     * 根据用户名和密码查询用户
     * @param userName
     * @param password
     * @return
     */
    @Select("select * from bus_user where bus_user.user_name = #{userName} and bus_user.password = #{password}")
    User findByUserNameAndPassword(@Param("userName") String userName,@Param("password") String password);

    @Insert("insert bus_user('user_name','password','email','phone','create_time','update_time') " +
            "values(#{user.userName},#{user.password},#{user.email},#{user.phone},#{user.createTime},#{user.updateTime})")
    void save(User user);

    @Update("UPDATE bus_user SET " +
            "user_name = #{userName}, " +
            "update_time = #{updateTime,jdbcType=TIMESTAMP}, " +
            "create_time = #{createTime,jdbcType=TIMESTAMP}, " +
            "email = #{email}, " +
            "password = #{password}, " +
            "phone = #{phone} " +
            "WHERE id = #{id}")
    void updateUser(@Param("userName") String userName, @Param("id") Integer id, @Param("updateTime") LocalDateTime updateTime, @Param("createTime") LocalDateTime createTime, @Param("email") String email, @Param("password") String password, @Param("phone") String phone);

    @Delete("delete from bus_user where id = #{id}")
    void deleteById(@Param("id") Integer id);

}
